using System.Diagnostics.CodeAnalysis;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Coderoom.ScrumPlanner.Data;
using Coderoom.ScrumPlanner.Data.Contracts;
using Coderoom.ScrumPlanner.Data.Repositories.Xml;
using StructureMap;

namespace Coderoom.ScrumPlanner.Web
{
	/// <summary>
	/// </summary>
	/// <remarks>
	///	For instructions on enabling IIS6 or IIS7 classic mode, visit http://go.microsoft.com/?LinkId=9394801
	/// </remarks>
	public class MvcApplication : HttpApplication
	{
		public MvcApplication()
		{
			ControllerBuilder.Current.SetControllerFactory(typeof (StructureMapControllerFactory));
		}

		[SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
		public void Application_Start()
		{
			RegisterRoutes(RouteTable.Routes);
			ConfigureStructureMapContainer();
		}

		public static void RegisterRoutes(RouteCollection routes)
		{
			routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

			#region Project Controller

			// example: ~/Projects
			routes.MapRoute("Projects", "Project", new {controller = "Project", action = "List"});

			// example: ~/Project/Detail/slug
			routes.MapRoute("Project Detail", "Project/Detail/{slug}", new { controller = "Project", action = "Detail" });

			// example: ~/Projects/List
			// example: ~/Projects/New
			routes.MapRoute("Projects Actions", "Project/{action}", new {controller = "Project", action = "List"});

			#endregion

			//#region Theme Controller

			//// example: ~/Themes
			//// example: ~/Themes/List
			//routes.MapRoute("Themes", "Themes/{action}", new {controller = "Theme", action = "List"});

			//#endregion

			//#region UserStory Controller

			//// example: ~/UserStories
			//// example: ~/UserStories/List
			//routes.MapRoute("UserStories", "UserStories/{action}", new {controller = "UserStory", action = "List"});

			//#endregion

			//#region Task Controller

			//// example: ~/Tasks
			//// example: ~/Tasks/List
			//routes.MapRoute("Tasks", "Tasks/{action}", new {controller = "Task", action = "List"});

			//#endregion

			//#region Sprint Controller

			//// example: ~/Sprints
			//// example: ~/Sprints/List
			//routes.MapRoute("Sprints", "Sprints/{action}", new {controller = "Sprint", action = "List"});

			//#endregion

			#region Home Controller

			// example: ~/
			routes.MapRoute("WebRoot", "", new {controller = "Home", action = "Index"});

			// example: ~/About
			routes.MapRoute("About", "About", new {controller = "Home", action = "About"});

			// example: ~/Search
			routes.MapRoute("Search", "Search", new {controller = "Home", action = "Search"});

			#endregion
		}

		public static void ConfigureStructureMapContainer()
		{
			ObjectFactory.Initialize(x =>
			{
				x.UseDefaultStructureMapConfigFile = false;
				x.Scan(o =>
				{
					o.AssemblyContainingType(typeof (MvcApplication));
					o.AddAllTypesOf<Controller>().NameBy(controller => controller.Name.Replace("Controller", string.Empty));
				});

				x.ForRequestedType<ProjectService>().TheDefaultIsConcreteType<ProjectService>();
				x.ForRequestedType<IProjectRepository>().TheDefaultIsConcreteType<XmlProjectRepository>();
			});
		}
	}
}